package com.fare.fdc.action;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.FreightPriceApplication;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.application.plane.city.PortApplication;
import com.fare.taluo.core.domain.config.channel.SourceEntityRepository;
import com.fare.taluo.core.domain.config.channel.impl.SourceEntityRepositoryImpl;
import com.fare.taluo.util.JsonUtil;
import com.fare.taluo.util.StringAndDateAndFileUtil;
import com.fare.taluo.vo.core.PlaneInfo;
import com.fare.taluo.vo.core.PortEntityVO;
import com.fare.taluo.vo.core.SourceEntityVO;

@Controller("pretrementQueryAction")
public class PretrementQueryAction extends BaseFdcAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7779394838017230459L;
	@Resource(name = "sourceApplication")
	private SourceApplication sourceApp;
	@Resource(name = "portApplication")
	private PortApplication portApp;
	@Resource(name="freightPriceApplication")
	private FreightPriceApplication freightPriceApplication;
	private List<PlaneInfo> allList;

	private List<PortEntityVO> portList;
	private List<SourceEntityVO> sourceList;
	private SourceEntityRepository sourceEntityRepository = new SourceEntityRepositoryImpl();

	private List<SourceEntityVO> uggsourceList;

	private String flightNumber; // 航班号
	private String depCity; // 起始城市
	private String arrCity; // 终点城市
	private String flightTime; // 航班时间
	private String grabChannel; // 渠道
	private String startTime; // 抓取时间段起始时间
	/*
	 * private String endTime; //抓取时间段终止时间
	 */private String carrier; // 航空公司
	private String areaCode; // 区域id
	private String pretrementTable;
	private String startKey;
	private String endKey;
	private String flightStopNum;

	public void init() {
		uggsourceList = sourceApp.findSourceEntityListByConditions("", "", "1",
				"", 1, 1000);
		portList = portApp.findAllPortEntityVO();
		sourceList = sourceApp.findAllSourceEntity();

	}

	public String toPretrementJSP() {
		init();

		returnPageURL = "/fare/pretrement.jsp";
		return "dispatcher";
	}

	public void pretrementQuery() throws Exception {
		allList = new ArrayList<PlaneInfo>();

		String fromCityStr = null;
		String toCityStr = null;
		String areaCity = null;
		String grabStartTime = null, flightStartTime = null, flightEndTime = null, grabEndTime = null;
		String hour = null;
		String[] family = null;
	    List<Filter> filters = new ArrayList<Filter>();
		depCity = URLDecoder.decode(depCity, "utf-8");// 出发地
		arrCity = URLDecoder.decode(arrCity, "utf-8");// 目的地
		grabChannel = URLDecoder.decode(grabChannel, "utf-8");// 渠道
		carrier = URLDecoder.decode(carrier, "utf-8");
		SourceEntityVO sourceVO = sourceApp.findByHbaseKey(carrier);
		String carrierKey = sourceVO.getCarrierKey();
		if (StringAndDateAndFileUtil.checkString(startTime)) {
			grabStartTime = startTime.replace("-", "").replace(" ", "");
			//grabEndTime = grabStartTime;
			grabEndTime = com.fare.util.DateUtil.getNextDateByDateStr(startTime, 1);
			filters.add(new SingleColumnValueFilter(//                     20130917093951552 													    
							Bytes.toBytes("planeinfo"),Bytes.toBytes("grabDate"),CompareOp.GREATER_OR_EQUAL,Bytes.toBytes(grabStartTime.replaceAll("-", "")+"000000000")));
			filters.add(new SingleColumnValueFilter(
					Bytes.toBytes("planeinfo"),Bytes.toBytes("grabDate"),CompareOp.LESS_OR_EQUAL,Bytes.toBytes(grabEndTime.replaceAll("-", "")+"000000000")));
		} else {
			grabStartTime = "00000000";
			grabEndTime = "99999999";
		}
		if (StringAndDateAndFileUtil.checkString(flightTime)) {
			/*filters.add(new SingleColumnValueFilter(Bytes.toBytes("planeinfo"),Bytes.toBytes("flightBatchDate")
					 ,CompareOp.EQUAL,Bytes.toBytes(flightTime.split(" ")[0])));*/
		    String compareStr = flightTime;
			flightStartTime = flightTime.replace("-", "");			
			//hour = flightStartTime.split(" ")[1];
			flightStartTime = flightStartTime;
			flightEndTime = flightStartTime;
			filters.add(new SingleColumnValueFilter(Bytes.toBytes("planeinfo"),Bytes.toBytes("flightBatchDate")
					 ,CompareOp.EQUAL,Bytes.toBytes(compareStr)));
			
		} else {
			flightStartTime = "00000000";
			flightEndTime = "99999999";
			hour = "00";
		}
		if (StringAndDateAndFileUtil.checkString(flightNumber)) {
 			flightStopNum = flightNumber;
			filters.add(new SingleColumnValueFilter(Bytes.toBytes("planeinfo"),Bytes.toBytes("flightId")
					 ,CompareOp.EQUAL,Bytes.toBytes(flightNumber))); 				          
		} else {
			flightNumber =  "000000";
			flightStopNum = "ZZZZZZ";
		}

		if (StringAndDateAndFileUtil.checkString(depCity)
				&& StringAndDateAndFileUtil.checkString(arrCity)) {
			// 返回航段
			PortEntityVO vo = portApp.findPortEntityByCityName(depCity);
			if (vo != null) {
				fromCityStr = vo.getPortCode();
				if (pretrementTable.equals("LP_FLIGHT_HISTORY")) {
					areaCode = String.format("%02d",
							Integer.parseInt(vo.getAreaCode()));
				}

			}
			PortEntityVO vo2 = portApp.findPortEntityByCityName(arrCity);
			if (vo2 != null) {
				toCityStr = vo2.getPortCode();
			}
		}

		if ("航空公司官网".equals(grabChannel)) {
			grabChannel = String.format("%05d", Integer.parseInt(carrier));
		}

		if (pretrementTable.equals("LP_FLIGHT")) {

			startKey = grabChannel + fromCityStr + toCityStr + "00"
					+ flightNumber + flightStartTime + "00";
			endKey = grabChannel + fromCityStr + toCityStr + "00"
					+ flightStopNum + flightEndTime + "24";

		} else if (pretrementTable.equals("LP_LINE_HOUR")) {
			startKey = grabChannel + fromCityStr + toCityStr + flightStartTime
					+ "00" + carrierKey;
			endKey = grabChannel + fromCityStr + toCityStr + flightEndTime
					+ "24" + carrierKey;

		} else if (pretrementTable.equals("LP_LINE_DAY")) {
			startKey = grabChannel + fromCityStr + toCityStr + flightStartTime
					+ carrierKey;
			endKey = grabChannel + fromCityStr + toCityStr + flightEndTime
					+ carrierKey;

		} else if (pretrementTable.equals("LP_FLIGHT_HISTORY")) {
			startKey = grabChannel + areaCode + fromCityStr + toCityStr
					+ grabStartTime + flightStartTime + "00" + "00"
					+ flightNumber;
			endKey = grabChannel + areaCode + fromCityStr + toCityStr
					+ grabEndTime + flightEndTime + "24" + "00" + flightStopNum;

		} else if (pretrementTable.equals("LP_FLIGHT_HISTORY_FIXED_CRAWLDATE")) {
			startKey = grabChannel + fromCityStr + toCityStr + "00"
					+ flightNumber + grabStartTime + flightStartTime + "00";
			endKey = grabChannel + fromCityStr + toCityStr + "00"
					+ flightStopNum + grabEndTime + flightEndTime + "24";

		}
		
		
		filters.add(new SingleColumnValueFilter(Bytes.toBytes("planeinfo"),Bytes.toBytes("carrierKey")
				 ,CompareOp.EQUAL,Bytes.toBytes(carrierKey)));
		/*ResultScanner scan = HBaseHelper.getRecords(pretrementTable, startKey,
				endKey,family,filters);
		try {
			allList = HBasePlaneinfoHelper
					.transformHBaseResultToPlaneinfo(scan);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		allList = freightPriceApplication.findByConditionsToPlaneInfo(pretrementTable, startKey,
				endKey,filters);
		String data = this.toJSON(allList, allList.size());
		super.toJSONOut(data);

	}

	public String toJSON(List list, int total) throws JsonGenerationException,
			JsonMappingException, IOException {
		String jsonStr = "{\"Total\":" + total + "," + "\"Rows\":";
		int i = 0;
		String json = JsonUtil.toJson(list);
		jsonStr += json;
		jsonStr += "}";
		return jsonStr;
	}

	public SourceApplication getSourceApp() {
		return sourceApp;
	}

	public void setSourceApp(SourceApplication sourceApp) {
		this.sourceApp = sourceApp;
	}

	public PortApplication getPortApp() {
		return portApp;
	}

	public void setPortApp(PortApplication portApp) {
		this.portApp = portApp;
	}

	public List<PlaneInfo> getAllList() {
		return allList;
	}

	public void setAllList(List<PlaneInfo> allList) {
		this.allList = allList;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public List<PortEntityVO> getPortList() {
		return portList;
	}

	public void setPortList(List<PortEntityVO> portList) {
		this.portList = portList;
	}

	public List<SourceEntityVO> getSourceList() {
		return sourceList;
	}

	public void setSourceList(List<SourceEntityVO> sourceList) {
		this.sourceList = sourceList;
	}

	public SourceEntityRepository getSourceEntityRepository() {
		return sourceEntityRepository;
	}

	public void setSourceEntityRepository(
			SourceEntityRepository sourceEntityRepository) {
		this.sourceEntityRepository = sourceEntityRepository;
	}

	public List<SourceEntityVO> getUggsourceList() {
		return uggsourceList;
	}

	public void setUggsourceList(List<SourceEntityVO> uggsourceList) {
		this.uggsourceList = uggsourceList;
	}

	public String getFlightNumber() {
		return flightNumber;
	}

	public void setFlightNumber(String flightNumber) {
		this.flightNumber = flightNumber;
	}

	public String getDepCity() {
		return depCity;
	}

	public void setDepCity(String depCity) {
		this.depCity = depCity;
	}

	public String getArrCity() {
		return arrCity;
	}

	public void setArrCity(String arrCity) {
		this.arrCity = arrCity;
	}

	public String getFlightTime() {
		return flightTime;
	}

	public void setFlightTime(String flightTime) {
		this.flightTime = flightTime;
	}

	public String getGrabChannel() {
		return grabChannel;
	}

	public void setGrabChannel(String grabChannel) {
		this.grabChannel = grabChannel;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	/*
	 * public String getEndTime() { return endTime; }
	 * 
	 * public void setEndTime(String endTime) { this.endTime = endTime; }
	 */

	public String getCarrier() {
		return carrier;
	}

	public void setCarrier(String carrier) {
		this.carrier = carrier;
	}

	public String getPretrementTable() {
		return pretrementTable;
	}

	public void setPretrementTable(String pretrementTable) {
		this.pretrementTable = pretrementTable;
	}


}
